package com.bsoft.gol.hcs.task;

import com.bsoft.gol.hcs.common.service.IFusMedicalRecordsService;
import ctd.controller.exception.ControllerException;
import ctd.job.annotation.ScheduleJob;
import ctd.job.pojo.JobParameter;
import ctd.util.JSONUtils;
import ctd.util.context.ContextUtils;
import hcn.common.Constants;
import hcn.util.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.bsoft.gol.hcs.satisfaction.service.ISatisfactionPlanService;
import com.bsoft.gol.hcs.specialist.service.IFollowUpAnswerService;
import com.bsoft.gol.hcs.specialist.service.IFollowUpPlanService;
import com.bsoft.gol.hcs.specialist.service.IPatientSearchRuleExecuteService;
import com.bsoft.gol.hcs.specialist.service.IProduceFusAfterPlanService;
import com.bsoft.gol.hcs.specialist.service.impl.ProduceSatisfactionSurveyPlanServiceImpl;
import org.atag.util.common.RedisUtil;
import org.atag.util.common.SpringPropertiesUtil;

import javax.annotation.Resource;


/**
 * 执行定时任务
 *
 * @author lxy
 * @date 2019/9/12
 */
@Component
public class Task {

	private final static Logger logger = LoggerFactory.getLogger(Task.class);
	@Resource
	IFusMedicalRecordsService fusMedicalRecordsService;
	@Resource(name = "followUpPlanService")
	private IFollowUpPlanService followUpPlanService;
	@Resource
	private IFollowUpAnswerService followUpAnswerService;
	@Resource
	private ISatisfactionPlanService satisfactionPlanService;
	@Autowired
	private ProduceSatisfactionSurveyPlanServiceImpl produceSatisfactionSurveyPlanService;
	@Resource
	private RedisUtil redisUtil;
	@Value("${project_source}")
	private String projectSource;
	@Resource(name = "patientSearchRuleExecuteService")
	private IPatientSearchRuleExecuteService patientSearchRuleExecuteService;
	@Resource
	private IProduceFusAfterPlanService produceFusAfterPlanService;

	/**
	 * @param
	 * @throws
	 * @description: 自动根据医生在医生站的设置生成院后随访计划
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/7/21 14:33
	 */
	@ScheduleJob(jobName = "autoProduceFusAfterPlanByDocSetting", cron = "0 0 4 * * ?", shardingTotalCount = 1,
			description =
					"自动根据医生在医生站的设置生成院后随访计划", overwrite = true)
	public void autoProduceFusAfterPlanByDocSetting(JobParameter jobParameter) {

		String tenantId = (String) ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId", SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoProduceFusAfterPlanByDocSetting start");
		try {
			produceFusAfterPlanService.produceFusAfterPlanByDocSetting();
		} catch (ControllerException e) {
			logger.error("autoProduceFusAfterPlanByDocSetting ControllerException:", e);
		}
		logger.info("autoProduceFusAfterPlanByDocSetting end");
	}

	/**
	 * @param
	 * @throws
	 * @description: 自动生成院后随访计划
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/7/21 14:33
	 */
	@ScheduleJob(jobName = "autoProduceFusAfterPlan", cron = "0 0 4 * * ?", shardingTotalCount = 1, description =
			"自动生成院后随访计划", overwrite = true)
	public void autoProduceFusAfterPlan(JobParameter jobParameter) {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoProduceFusAfterPlan start");
		try {
			produceFusAfterPlanService.produceFusAfterPlan(null);
		} catch (ControllerException e) {
			logger.error("autoProduceFusAfterPlan ControllerException:", e);
		}
		logger.info("autoProduceFusAfterPlan end");
	}

	/**
	 * @param
	 * @throws
	 * @description: 自动生成所有随访计划记录
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/7/21 14:33
	 */
	@ScheduleJob(jobName = "autoProduceExecutionList", cron = "0 0 4 * * ?", shardingTotalCount = 1, description =
			"自动生成所有随访计划记录", overwrite = true)
	public void autoProduceExecutionList(JobParameter jobParameter) {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoProduceExecutionList start");
		try {
			followUpPlanService.produceExecutionListAll();
		} catch (ControllerException e) {
			e.printStackTrace();
		}
		logger.info("autoProduceExecutionList finish result:{}");
	}

	/**
	 * @param
	 * @throws
	 * @description: 自动终止随访计划
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/7/29 17:53
	 */
	@ScheduleJob(jobName = "autoStopExecutionList", cron = "0 0 * * * ?", shardingTotalCount = 1, description =
			"自动终止随访计划", overwrite = true)
	public void autoStopExecutionList(JobParameter jobParameter) throws ControllerException {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoStopExecutionList start");
		followUpPlanService.autoStopExecutionList();
		logger.info("autoStopExecutionList finish result:{}");

	}

	/**
	 * @param
	 * @throws
	 * @description: 自动更新指标结果内容
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/7/29 17:53
	 */
	@ScheduleJob(jobName = "autoUpdateIndexResultContent", cron = "30 20 * * * ?", shardingTotalCount = 1,
			description = "自动更新指标结果内容", overwrite = true)
	public void autoUpdateIndexResultContent(JobParameter jobParameter) throws ControllerException {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoUpdateIndexResultContent start");
		followUpAnswerService.autoUpdateIndexResultContent();
		logger.info("autoUpdateIndexResultContent finish result:{}");
	}

	/**
	 * @param
	 * @throws
	 * @description: 自动同步表单编辑器答案
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/7/29 17:53
	 */
	@ScheduleJob(jobName = "autoSyncEditorAnswer", cron = "0 0/15 * * * ?", shardingTotalCount = 1, description =
			"自动同步表单编辑器答案", overwrite = true)
	public void autoSyncEditorAnswer(JobParameter jobParameter) throws ControllerException {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoSyncEditorAnswer start");
		followUpAnswerService.syncEditorAnswer();
		logger.info("autoSyncEditorAnswer finish result:{}");
	}

	/**
	 * @param
	 * @throws
	 * @description: 自动发送满意度随访消息  0 0 8/12 * * ?
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/9/3 16:20
	 */
	@ScheduleJob(jobName = "autoSendSatisfactionSurveyMessage", cron = "0 0 20 * * ?", shardingTotalCount = 1,
			description =
					"自动发送满意度随访消息", overwrite = true)
	public void autoSendSatisfactionSurveyMessage(JobParameter jobParamete) {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoSendSatisfactionSurveyMessage start");
		BaseResponse baseResponse = satisfactionPlanService.sendSatisfactionSurveyMessageToAll();
		logger.info("autoSendSatisfactionSurveyMessage finish result:{}", baseResponse);
	}




	/**
	 * @param
	 * @throws
	 * @description: 自动生成满意度随访计划
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/9/8 17:39
	 */
	@ScheduleJob(jobName = "autoProduceSatisfactionSurvey", cron = "0 0 19 * * ?", shardingTotalCount = 1,
			description =
					"自动生成满意度随访计划", overwrite = true)
	public void autoProduceSatisfactionSurvey(JobParameter jobParamete) throws ControllerException {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoProduceSatisfactionSurvey start");
		produceSatisfactionSurveyPlanService.producePlan();
		logger.info("autoProduceSatisfactionSurvey end");
	}
	@ScheduleJob(jobName = "autoProduceSatisfactionSurveyForMenZhen", cron = "0 30 19 * * ?", shardingTotalCount = 1,
			description =
					"自动生成满意度随访计划", overwrite = true)
	public void autoProduceSatisfactionSurveyForMenZhen(JobParameter jobParamete) throws ControllerException {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoProduceSatisfactionSurvey start");
		produceSatisfactionSurveyPlanService.producePlan();
		logger.info("autoProduceSatisfactionSurvey end");
	}
	/**
	 * @param
	 * @throws
	 * @description: 自动生成满意度随访计划
	 * @return: void
	 * @author: tongtp
	 * @date: 2020/9/8 17:39
	 */
	@ScheduleJob(jobName = "autoProduceSatisfactionSurveyForUser", cron = "0 30 19 * * ?", shardingTotalCount = 1,
			description =
					"自动生成满意度随访计划-住院", overwrite = true)
	public void autoProduceSatisfactionSurveyForUser(JobParameter jobParamete) throws ControllerException {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoProduceSatisfactionSurvey start");
		produceSatisfactionSurveyPlanService.producePlan();
		logger.info("autoProduceSatisfactionSurvey end");
	}


	//	/**
//	 * 每天凌晨3点执行 ，同步就诊记录
//	 *
//	 * @throws Exception
//	 */
//	@RpcService
//	public void synchronizationMedicalRecords() throws Exception {
//		fusMedicalRecordsService.synchronizationMedicalRecords();
//	}
//	@ScheduleJob(jobName = "autoFollowUpRecords", cron = "0 0 6 * * ?", shardingTotalCount = 1, description =
@ScheduleJob(jobName = "autoFollowUpRecords", cron = "0 0 6 * * ?", shardingTotalCount = 1, description =
		"随访任务推送通知消息", overwrite = true)
public void autoFollowUpRecords(JobParameter jobParameter) throws Exception {

	String tenantId = (String)ContextUtils.get("$tenantId");
	if(null == tenantId){
		ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
	}

	logger.info("autoFollowUpRecords start");
	followUpPlanService.autoFollowUpRecords();
	logger.info("autoFollowUpRecords end");
}

	/**
	 * 自动同步检验报告定时任务
	 *
	 * @param
	 * @return void
	 * @throws
	 * @author tongtp
	 * @date 2021/8/10 21:14
	 */
//	@ScheduleJob(jobName = "autoSyncLisInspectionReport", cron = "0 0 0 * * ?", shardingTotalCount = 1, description =
//			"自动同步检验报告定时任务", overwrite = true)
//	public void autoSyncLisInspectionReport(JobParameter jobParameter) {
//		logger.info("autoSyncLisInspectionReport start");
//		try {
//			lisInspectionReportService.syncLisInspectionReport();
//		} catch (Exception e) {
//			logger.error("autoSyncLisInspectionReport error:{}", e);
//		}
//		logger.info("autoSyncLisInspectionReport end");
//	}

	/**
	 * 自动删除已失效的检验报告
	 *
	 * @param
	 * @return void
	 * @throws
	 * @author tongtp
	 * @date 2021/8/10 21:14
	 */
//	@ScheduleJob(jobName = "autoDeleteExpiredInspectionReport", cron = "0 0 23 * * ?", shardingTotalCount = 1,
//			description = "自动删除已失效的检验报告", overwrite = true)
//	public void autoDeleteExpiredInspectionReport(JobParameter jobParameter) {
//		logger.info("autoDeleteExpiredInspectionReport start");
//		try {
//			lisInspectionReportService.deleteExpiredInspectionReport();
//		} catch (ControllerException e) {
//			logger.error("autoDeleteExpiredInspectionReport error:{}", e);
//		}
//		logger.info("autoDeleteExpiredInspectionReport end");
//	}

	/**
	 * 自动处理已订阅的患者检索规则定时任务
	 *
	 * @param
	 * @return void
	 * @throws
	 * @author tongtp
	 * @date 2021/8/10 21:14
	 */
	@ScheduleJob(jobName = "autoHandlePatientSearchRuleForSubscribe", cron = "0 0 3 * * ?", shardingTotalCount = 1,
			description = "自动处理已订阅的患者检索规则定时任务", overwrite = true)
	public void autoHandlePatientSearchRuleForSubscribe(JobParameter jobParameter) {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autohandlePatientSearchRuleForSubscribe start");
		try {
			patientSearchRuleExecuteService.handlePatientSearchRuleForSubscribe();
		} catch (ControllerException e) {
			logger.error("autohandlePatientSearchRuleForSubscribe error:{}", e);
		}
		logger.info("autohandlePatientSearchRuleForSubscribe end");
	}

	/**
	 * 自动给患者发送当天院后随访需要健康宣教消息的定时任务
	 *
	 * @param
	 * @return void
	 * @throws
	 * @author tongtp
	 * @date 2021/8/10 21:14
	 */
	@ScheduleJob(jobName = "autoHandleHospitaHealthEducationForSubscribe", cron = "0 0 9 * * ?", shardingTotalCount
			= 1,
			description = "自动给患者发送当天院后随访需要健康宣教消息的定时任务", overwrite = true)
	public void autoHandleHospitalHealthEducationForSubscribe(JobParameter jobParameter) {

		String tenantId = (String)ContextUtils.get("$tenantId");
		if(null == tenantId){
			ContextUtils.put("$tenantId",SpringPropertiesUtil.get("tenantId"));
		}

		logger.info("autoHandleHospitalHealthEducationForSubscribe start");
		try {
			patientSearchRuleExecuteService.autoHandleHospitalHealthEducationForSubscribe();
		} catch (ControllerException e) {
			logger.error("autoHandleHospitalHealthEducationForSubscribe error:{}", e);
		}
		logger.info("autoHandleHospitalHealthEducationForSubscribe end");
	}
}
